<?php

class pageComponents extends sfComponents
{
  public function executeHomeAuthors($number)
  {
    $this->authors = array();
    foreach(Tools::$letters as $letter)
    {
      $count = AuthorQuery::create()
        ->filterbyContent(null, Criteria::NOT_EQUAL)
        ->filterByLetter($letter)
        ->count();
      $rand = rand(0, $count-3);

      $this->authors[$letter] = AuthorQuery::create()
        ->setOffset($rand)
        ->limit(3)
        ->filterBycontent(null,Criteria::NOT_EQUAL)
        ->findByLetter($letter);
    }
  }

  public function executeWebStatistics()
  {
    $browser  =$_SERVER['HTTP_USER_AGENT']; // get the browser name
    $ip  =  $_SERVER['REMOTE_ADDR'];   // get the IP address
    $from_page = $_SERVER['HTTP_REFERER'];//  page from which visitor came
    $page=$_SERVER['PHP_SELF'];//get current page Read more at http://www.devshed.com/c/a/PHP/Tracking-Website-Statistics-with-PHP/#0d7gGgG69XtZHl1a.99

    $existing_engines = array();
    foreach (Tools::getSearchEngines() as $keyword => $search_engine)
    {
      if (strpos($browser, $keyword))
        $existing_engines[] = $search_engine;
    }

    if (empty($existing_engines))
    {
      $statistic = new Statistic();
      $statistic->setIp($ip);
      $statistic->setPage($page);
      if ($from_page)
        $statistic->setFromPage($from_page);
      $statistic->setBrowser($browser);
      $statistic->save();
    }
    else
    {
      foreach ($existing_engines as $engine)
      {
        $search_engine = SearchEngineQuery::create()->filterByName($engine)->findOneOrCreate();
        $search_engine->incrementCountAndSave();
      }
    }
  }
}
